﻿{
  "metadata": {
    "name": "london-data-analyse",
    "kernelspec": {
      "language": "scala",
      "name": "spark2-scala"
    },
    "language_info": {
      "codemirror_mode": "text/x-scala",
      "file_extension": ".scala",
      "mimetype": "text/x-scala",
      "name": "scala",
      "pygments_lexer": "scala"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2,
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# 基于london的天气数据进行可视化分析"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## 导入依赖库"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n%matplotlib inline    \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nmpl.rcParams[\u0027font.sans-serif\u0027] \u003d [\u0027KaiTi\u0027, \u0027SimHei\u0027, \u0027FangSong\u0027]  # 汉字字体,优先使用楷体，如果找不到楷体，则使用黑体\nmpl.rcParams[\u0027font.size\u0027] \u003d 12  # 字体大小\nmpl.rcParams[\u0027axes.unicode_minus\u0027] \u003d False  # 正常显示负号"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## Getting the data"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather \u003d pd.read_csv(\u0027https://gitee.com/cloudcoder/data-visual/raw/master/london/london2018.csv\u0027)\nprint(weather)"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "### 简单示例\n"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(y\u003d\u0027Tmax\u0027, x\u003d\u0027Month\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(y\u003d[\u0027Tmax\u0027,\u0027Tmin\u0027], x\u003d\u0027Month\u0027,kind\u003d\u0027line\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather[\u0027Tmed\u0027] \u003d (weather[\u0027Tmax\u0027] + weather[\u0027Tmin\u0027])/2\nweather.plot.line(y\u003d[\u0027Tmax\u0027,\u0027Tmin\u0027,\u0027Tmed\u0027], x\u003d\u0027Month\u0027,grid\u003dTrue)\nplt.show()"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "### 柱状图"
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027bar\u0027, y\u003d\u0027Rain\u0027, x\u003d\u0027Month\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027barh\u0027, y\u003d\u0027Rain\u0027, x\u003d\u0027Month\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027bar\u0027, y\u003d[\u0027Tmax\u0027,\u0027Tmin\u0027], x\u003d\u0027Month\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027bar\u0027, y\u003d[\u0027Tmax\u0027,\u0027Tmed\u0027,\u0027Tmin\u0027], x\u003d\u0027Month\u0027)\nplt.show()"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "### 散点图 与 饼图"
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027scatter\u0027, x\u003d\u0027Sun\u0027, y\u003d\u0027Rain\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027pie\u0027, y\u003d\u0027Sun\u0027)\nplt.show()\n"
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.index\u003d[\u0027一月\u0027,\u0027二月\u0027,\u0027三月\u0027,\u0027四月\u0027,\u0027五月\u0027,\u0027六月\u0027,\u0027七月\u0027,\u0027八月\u0027,\u0027九月\u0027,\u0027十月\u0027,\u0027十一月\u0027,\u0027十二月\u0027]\nweather.plot(kind\u003d\u0027pie\u0027, y \u003d \u0027Rain\u0027, legend\u003dFalse)\nplt.show()"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## Statistical charts and spotting unusual events"
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nmore_weather \u003d pd.read_csv(\u0027https://gitee.com/cloudcoder/data-visual/raw/master/london/londonweather.csv\u0027)\nprint(more_weather[0:48])"
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nprint(more_weather.Rain.describe())"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## Histograms"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## Pandas Plot utilities"
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nmore_weather.plot(kind\u003d\u0027hist\u0027, y\u003d\u0027Rain\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nmore_weather.plot(kind\u003d\u0027hist\u0027, y\u003d\u0027Rain\u0027, bins\u003d[0,25,50,75,100,125,150,175])\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nmore_weather.plot.hist(y\u003d\u0027Rain\u0027, bins\u003d[0,25,75,175])\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nmore_weather.plot.box(y\u003d\u0027Rain\u0027)\nplt.show()"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "### Multiple charts"
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027bar\u0027, y\u003d[\u0027Tmax\u0027, \u0027Tmin\u0027,\u0027Rain\u0027,\u0027Sun\u0027], subplots\u003dTrue, layout\u003d(2,2), figsize\u003d(10,5))\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(y\u003d[\u0027Tmax\u0027, \u0027Tmin\u0027,\u0027Rain\u0027,\u0027Sun\u0027], subplots\u003dTrue, layout\u003d(2,2), figsize\u003d(10,5),\ntitle\u003d\u0027The Weather of London\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n\nnew_df \u003d weather[[\u0027Tmax\u0027,\u0027Tmin\u0027,\u0027Rain\u0027,\u0027Sun\u0027]]\nnew_df.plot.pie(subplots\u003dTrue, figsize\u003d(32, 8), autopct\u003d\u0027%1.1f%%\u0027, legend\u003dFalse)\nplt.show()"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "### Saving the Charts"
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nweather.plot(kind\u003d\u0027pie\u0027, y\u003d\u0027Rain\u0027, legend\u003dFalse)\nplt.show()\nplt.savefig(\"pie.png\")\n"
    },
    {
      "cell_type": "raw",
      "metadata": {
        "format": "text/plain"
      },
      "source": "%python\n"
    }
  ]
}